<template>
  <div style="position:relative;min-height:100vh;">
    <div class="login-logo">
      <img class="login-logo__img" src="@/assets/images/hsy-logo.png" alt="logo">
    </div>
    <div class="login-form">
      <van-cell-group>
        <van-field
          v-model="model.userName.value"
          type="tel"
          required
          clearable
          :label="model.userName.label"
          placeholder="请输用户号"
        />
        <van-field
          v-model="model.password.value"
          type="password"
          :label="model.password.label"
          placeholder="请输入密码"
          required
          clearable
        />
      </van-cell-group>
      <div class="mtb-30 plr-20">
        <van-button type="danger" size="large" @click="submit">登录</van-button>
      </div>
      <!-- <p class="tac">或</p>
      <div class="mtb-30 plr-20">
        <van-button type="default" size="large" to="/" replace>游客浏览</van-button>
      </div>-->
      <div class="plr-30 flex flex-s-b color-link">
        <router-link to="/register" replace class="pr-20">快速注册</router-link>
        <router-link to="/forgetPassword" replace>忘记密码</router-link>
      </div>
      <div class="mtb-30 ptb-30 tac">
        <p class="color-gray-light">——&#x3000;其他登录方式&#x3000;——</p>
        <div class="smsLogin">
          <router-link to="/smsLogin" replace>
            <svg-icon icon-class="phone" />
            <p>短信验证码登录</p>
          </router-link>
        </div>
      </div>
    </div>

    <div class="has-bottom" />
    <!-- 协议 -->
    <div class="mod-protocol flex flex-h-c">
      <van-checkbox v-model="checked" checked-color="#f44" icon-size="0.4rem" label-disabled>
        <span>登录注册表示您已阅读并同意</span>
        <router-link to="/userProtocol" class="color-danger">《用户协议》</router-link>
        <span>和</span>
        <router-link to="/userPrivacy" class="color-danger">《用户隐私》</router-link>
      </van-checkbox>
    </div>
  </div>
</template>

<script>
import { login } from 'api/member'

export default {
  data() {
    return {
      model: {
        userName: {
          label: '用户号',
          value: '',
          require: true,
          requireMessage: '请输入用户号'
          // pattern: /^1[3456789]\d{9}$/,
          // patternMessage: '请输入正确的手机号'
        },
        password: {
          label: '登录密码',
          value: '',
          require: true,
          requireMessage: '请输入密码'
        }
      },
      checked: true,
      redirect: ''
    }
  },
  created() {
    // 判断是否从某个页面跳转到登录页
    if (this.$route.query.redirect) {
      this.redirect = this.$route.query.redirect
    }
  },
  methods: {
    fetchData() {
      getInfo().then(response => {
        console.log(response.data)
      })
    },
    submit() {
      const self = this
      // 表单验证
      if (self.$utils.formValidate(self.model).valid) {
        // 验证通过
        if (self.checked) {
          login(self.model).then(res => {
            // console.log(res);
            if (res) {
              this.$toast({
                type: 'success',
                message: '登录成功',
                duration: 1000,
                forbidClick: true,
                onClose: function() {
                  // 登录成功，设置token
                  self.$store.dispatch('user/setToken', res.Data)
                  self.$store.dispatch('user/login', true)
                  localStorage.setItem('Token', res.Data.Token)
                  localStorage.setItem('RefreshToken', res.Data.RefreshToken)
                  localStorage.setItem('isLogin', '1')
                  self.$router.replace({
                    path: self.$store.getters.redirect ? self.$store.getters.redirect : '/'
                  })
                }
              })
            }
          })
        } else {
          this.$toast('请同意用户协议与用户隐私！')
        }
      } else {
        this.$toast(self.$utils.formValidate(self.model).msg)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.smsLogin {
  margin-top: 50px;
  color: #f44;
  .svg-icon {
    font-size: 50px;
  }
}
</style>

